<?php if ( ! defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
/**
 * BackendPro
 *
 * A website backend system for developers for PHP 4.3.2 or newer
 *
 * @package         conlabz CMS
 * @author          Christian Schneider
 * @copyright       Copyright (c) 2009
 * @license         
 * @link            http://www.conlabz.de
 * @filesource
 */

// ---------------------------------------------------------------------------

/**
 * Personfunction_model
 *
 * Model used to retrive webites
 *
 * @package			conlabz CMS
 * @subpackage		Models
 */
class Personfunction_model extends Base_model {
	function Personfunction_model( ) {
		parent::Base_model( );

		$this->_TABLES = array( 'Option' => $this->config->item( 'backendpro_table_prefix' ) . 'article' );

		// Cache to store already fetched items
		$this->_CACHE = array( );

		// Object keyword
		// I wouldn't advise changing this, it could corrupt current
		// articles, the reason for needing this is to stop unserialze
		// errors spamming the log files
		$this->object_keyword = "BeP::Object::";

		log_message( 'debug', 'conlabz CMS : Personfunction_model class loaded' );
	}

	/**
	 * Get Blogs
	 *
	 * @access public
	 * @param mixed $where Where query string/array
	 * @param array $limit Limit array including offset and limit values
	 * @return object
	 */
	function getPersonFunctions( $where = NULL, $limit = array( 'limit' => NULL, 'offset' => '' ) ) {
		$this->db->select( 'personfunction.id, 
							personfunction.title, 
							orgunit.name' 
						 );
		$this->db->from( "personfunction" );
		$this->db->from( "category" );
		$this->db->from( "orgunit" );
		$this->db->from( "orgunit_xref" );
		$this->db->where( "`personfunction`.`id` = `orgunit_xref`.`function_ref`
						   AND `orgunit_xref`.`orgunit_ref` = `orgunit`.`id`
						   AND `orgunit`.`category_id` = `category`.`id` 
						   AND `orgunit`.`category_language_id` = `category`.`language_id` 
						   AND `category`.`website_id` = ", $this->session->userdata['website_id'] );
		if( !is_null( $where ) ) {
			$this->db->where( $where );
		}
		if( !is_null( $limit['limit'] ) ) {
			$this->db->limit( $limit['limit'], ( isset( $limit['offset'] ) ? $limit['offset'] : '' ) );
		}
		$this->db->order_by('personfunction.title desc'); 
		return $this->db->get( );
	}
	
	/**
	 * Insert new Blog
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function insert( $data = array( ) ) {
		//create new personfunction entry
		$this->db->set( 'id',          $data['id']          );
		$this->db->set( 'title',        $data['title']        );
		$this->db->set( 'email',      $data['email']      );
		$this->db->set( 'description', $data['description'] );

		//insert the new blog in the blog DB
		$this->db->insert( 'personfunction' );
		
		$personfunction_id = $this->db->insert_id( );
		
		$this->db->set( 'orgunit_ref', $data['orgunit_id'] );
		$this->db->set( 'function_ref', $personfunction_id );
		
		$this->db->insert( 'orgunit_xref' );
		
		return $personfunction_id;
	}
	
	/**
	 * Update Blog
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function update( $data = array( ), $id ) {		
		$blog = array( );
		$blog['title']        = $data['title'];
		$blog['email']      = $data['email'];
		$blog['description'] = $data['description'];

		//insert the new personfunction
		$this->db->update( 'personfunction', $personfunction, array( 'id' => $id ) );
		//@todo: update orgunit_xref
	}

	/**
	 * Constructs an array of the given blogs to be used for a dropdown menu
	 *
	 * @access public
	 * @return array
	 */
	function buildPersonFunctionDropdown( ) {
		$personfunctions = $this->getPersonFunctions( );
		
		$dropdown = array( );
		foreach( $personfunctions->result( ) as $row ) {
	    	 $dropdown[$row->id] = $row->title;
		}
		
		return $dropdown;
	}
}
/* End of file personfunction_model.php */
/* Location: ./modules/people/models/personfunction_model.php */
